<?php
/**
 * Created by PhpStorm.
 * User: Cloud
 * Date: 2018/4/9
 * Time: 10:22
 */

class Office extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->library('session');
        $this->load->helper('url');
        $this->load->helper('json');
        $this->load->library('pagination');
        $this->load->model('customer_model');
        $this->load->model('goods_model');
    }

    //商品列表页面
    public function index()
    {
        $datas['p'] = isset($_GET['p']) ? $_GET['p'] : 0;
        $datas['c'] = isset($_GET['c']) ? $_GET['c'] : 0;
        $datas['b'] = isset($_GET['b']) ? $_GET['b'] : 0;

        $customer_id = $this->session->customer_id;
        //分类获取
        $url = $this->config->item("shop_url") . 'companymanagerapi/company_goods_classify/';
        $data_arr['time'] = time();
        $data_arr['key'] = md5($this->config->item("token_const_wjyh") . $data_arr['time']);
        $data_arr['uid'] = $customer_id;
        $rs = curl_request($url, $data_arr);
        $rs = json_decode($rs, true);
        $cass = array();
        $big = array();
        if ($rs['code'] == 200) {
            $datas['datas'] = $rs['data'];
            foreach ($datas['datas'] as $key => $call) {
                if (isset($call['data'])) {
                    $big[$call['cate_big_id']] = $call['cate_big_name'];
                }
            };
            foreach ($datas['datas'] as $key => $call) {
                if (isset($call['data'])) {
                    foreach ($call['data'] as $key1 => $small) {
                        $cass[$call['cate_big_id']][$small['cate_small_id']] = $small['cate_small_name'];
                        // $cass[$call['cate_big_id']][$call1['cate_small_id']]=$call1['cate_small_id'];
                        $cass[$call['cate_big_id']][0] = '不限';
                        ksort($cass[$call['cate_big_id']]);
                    }
                }
            }
        }
        //获取用户的admin在商城那边的shop_user_id
        $admin_shop_user_id = $this->user_model->customer_id_to_admin_to_shop_user_id($customer_id);
        //获取商品接口
        $urlpro = $this->config->item("shop_url") . 'companymanagerapi/company_search_goods/';
        $data_pro['time'] = time();
        $data_pro['shop_user_id'] = $admin_shop_user_id;
        $data_pro['key'] = md5($this->config->item("token_const_wjyh") . $data_pro['time']);

        $datas['big'] = $big;
        $datas['big'][0] = '不限';
        ksort($datas['big']);

        $datas['p_n'] = false;
        if ($datas['big'] && $datas['p']) {
            if (count($datas['big'])) {
                foreach ($datas['big'] as $k => $v) {
                    if ($k == $datas['p']) {
                        $datas['p_n'] = $v;
                        break;
                    }
                }
            }
        }


        if ($datas['p']) {
            if (count($datas['datas'][$datas['p']]['data'])) {
                $datas['small'][0] = '不限';
                foreach ($datas['datas'][$datas['p']]['data'] as $k => $v) {
                    $datas['small'][$v['cate_small_id']] = $v['cate_small_name'];
                }
            }
        } else {
            $datas['small'] = null;
        }

        $datas['c_n'] = false;
        if ($datas['small'] && $datas['c']) {
            if (count($datas['small'])) {
                foreach ($datas['small'] as $k => $v) {
                    if ($k == $datas['c']) {
                        $datas['c_n'] = $v;
                        break;
                    }
                }
            }
        }

        //获取全部品牌
        $url = $this->config->item("shop_url") . 'companymanagerapi/company_pc_brand/';
        $data_arr['time'] = time();
        $data_arr['key'] = md5($this->config->item("token_const_wjyh") . $data_arr['time']);
        if ($datas['p']) {
            $data_arr['cate_big_id'] = $datas['p'];
            if ($datas['c']) {
                $data_arr['cate_small_id'] = $datas['c'];
            }
        }
        $rs = curl_request($url, $data_arr);
        $rs = json_decode($rs, true);

        $datas['b_n'] = false;
        if ($rs['code'] == 200) {
            $datas['brand'] = $rs['data'];
//            $datas['brand'][0] = array('brand_id'=>0,'brand_name'=>'不限');
            if (count($datas['brand']) && $datas['b']) {
                foreach ($datas['brand'] as $v) {
                    if ($v['brand_id'] == $datas['b']) {
                        $datas['b_n'] = $v['brand_name'];
                        break;
                    }
                }
            }
        }

        $data_arr = array();
        //获取当前页商品
        if (isset($_GET['curPage'])) {
            $pag_id = $_GET['curPage'];
        } else {
            $pag_id = 1;
        }
        $url = $this->config->item("shop_url") . 'companymanagerapi/company_search_goods/';
        $data_arr['time'] = time();
        $data_pro['shop_user_id'] = $admin_shop_user_id;
        $data_arr['key'] = md5($this->config->item("token_const_wjyh") . $data_arr['time']);
        $data_arr['curPage'] = $pag_id;
        $rs2 = curl_request($url, $data_arr);
        $rs1 = json_decode($rs2, true);
        if ($rs1['code'] == 200) {
            $datas['goods'] = $rs1['data'];
            $datas['total'] = $rs1['total'];
        }

        if (isset($_GET['keywords'])) {
            $data_pro['keywords'] = $_GET['keywords'];
            $datas['keywords'] = $_GET['keywords'];
        } else {
            $datas['keywords'] = '';
        }
        if (isset($_GET['endPrice'])) {
            $data_pro['endPrice'] = $_GET['endPrice'];
        }
        if (isset($_GET['startPrice'])) {
            $data_pro['startPrice'] = $_GET['startPrice'];
        }

        if (isset($_GET['orderAttribute'])) {
            $data_pro['orderAttribute'] = $_GET['orderAttribute'];
        }

        if (isset($_GET['curPage'])) {
            $data_pro['curPage'] = $_GET['curPage'];
        }

        $data_pro['p_cat_id'] = $datas['p'];
        $data_pro['c_cat_id'] = $datas['c'];

        if (isset($_GET['order'])) {
            $data_pro['order'] = $_GET['order'];
            if ($_GET['order'] == 'asc') {
                $datas['order'] = 'desc';
            } else {
                $datas['order'] = 'asc';
            }
        } else {
            $datas['order'] = 'asc';
        }

        $data_pro['brand_id'] = $datas['b'];

        $rs1 = curl_request($urlpro, $data_pro);
        $rs = json_decode($rs1, true);
        if ($rs['code'] == 200) {
            $datas['goods'] = $rs['data'];
            $datas['total'] = isset($rs['total']) ? $rs['total'] : 0;
        }

        $datas['url'] = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

        if (isset($_GET['orderAttribute']) && $_GET['orderAttribute']) {
            $datas['orderAttribute'] = $_GET['orderAttribute'];
        } else {
            $datas['orderAttribute'] = '';
        }

        //分页
        if (!empty($pag_id)) {
            $pag = ($pag_id - 1) * 60;
            $datas['pag'] = $pag;
            $datas['pag_id'] = $pag_id;
        } else {
            $datas['pag_id'] = 1;
            $datas['pag'] = 0;
        }
        $datas['type'] = 2;
        $datas['pages'] = ceil($datas['total'] / 60);
        $this->load->view('office/index.php', $datas);
    }

    public function lists()
    {
//        获取商品接口
        $urlpro = $this->config->item("shop_url") . 'companymanagerapi/company_limit_bill/';
        $data_pro['time'] = time();
        $data_pro['key'] = md5($this->config->item("token_const_wjyh") . $data_pro['time']);


        $data_arr = array();
        //获取当前页商品
        if (isset($_GET['curPage'])) {
            $pag_id = $_GET['curPage'];
        } else {
            $pag_id = 1;
        }

        if (isset($_GET['keywords'])) {
            $data_pro['keywords'] = $_GET['keywords'];
            $datas['keywords'] = $_GET['keywords'];
        } else {
            $datas['keywords'] = '';
        }
        if (isset($_GET['endPrice'])) {
            $data_pro['endPrice'] = $_GET['endPrice'];
        }
        if (isset($_GET['startPrice'])) {
            $data_pro['startPrice'] = $_GET['startPrice'];
        }

        if (isset($_GET['orderAttribute'])) {
            $data_pro['orderAttribute'] = $_GET['orderAttribute'];
        }

        if (isset($_GET['curPage'])) {
            $data_pro['curPage'] = $_GET['curPage'];
        }


        if (isset($_GET['order'])) {
            $data_pro['order'] = $_GET['order'];
            if ($_GET['order'] == 'asc') {
                $datas['order'] = 'desc';
            } else {
                $datas['order'] = 'asc';
            }
        } else {
            $datas['order'] = 'asc';
        }

        $customer_id = $this->session->customer_id;

        if ($customer_id) {
            $identity = $this->session->customer_info['identity'];
            if ($identity == 1) {
                $shop_user_id = $this->user_model->customer_id_to_shop_user_id($customer_id);
            }
            if ($identity == 2) {
                $parent_id = $this->user_model->customer_id_to_parent_id($customer_id);
                $shop_user_id = $this->user_model->customer_id_to_shop_user_id($parent_id);
            }
            if ($identity == 3) {
                $parent_id = $this->user_model->customer_id_to_admin_id($customer_id);
                $shop_user_id = $this->user_model->customer_id_to_shop_user_id($parent_id);
            }


            $data_pro['uid'] = $shop_user_id;

            $list_ids = $this->customer_model->u_m_list_ids($this->session->customer_id);
            if ($list_ids && count($list_ids)) {
                $data_pro['tempid'] = implode(',', $list_ids);
            } else {
                $data_pro['tempid'] = '';
            }
        } else {

        }

        $rs1 = curl_request($urlpro, $data_pro);
        $rs = json_decode($rs1, true);
        $datas['total'] = 0;
        $datas['goods'] = array();
        if ($rs['code'] == 200) {
            $datas['goods'] = $rs['data'];
            $datas['total'] = $rs['total'];
        }

        $datas['url'] = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

        if (isset($_GET['orderAttribute']) && $_GET['orderAttribute']) {
            $datas['orderAttribute'] = $_GET['orderAttribute'];
        } else {
            $datas['orderAttribute'] = '';
        }

        //分页
        if (!empty($pag_id)) {
            $pag = ($pag_id - 1) * 60;
            $datas['pag'] = $pag;
            $datas['pag_id'] = $pag_id;
        } else {
            $datas['pag_id'] = 1;
            $datas['pag'] = 0;
        }
        $datas['type'] = 1;
        $datas['pages'] = ceil($datas['total'] / 60);
        $this->load->view('office/lists.php', $datas);
    }


}